<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Habit extends MY_Controller {

	
	public function home($child_id)
	{

        //孩子信息
        $this->load->model('User_child_model');
        $child_info = $this->User_child_model->get_info_by_id($child_id);
        $grade_array = array(
            '0'=>'学前班',
            '1'=>'一年级',
            '2'=>'二年级', 
            '3'=>'三年级', 
            '4'=>'四年级', 
            '5'=>'五年级', 
            '6'=>'六年级', 
            '7'=>'七年级', 
            '8'=>'八年级', 
            '9'=>'九年级', 
            '10'=>'高一', 
            '11'=>'高二', 
            '12'=>'高三', 
            '13'=>'其他', 
        );
        // print_r($child_info) ;exit;
		$habit_count = $this->User_child_model->get_habit_count($child_id); 
        //返回数组
        $info = array(
            'child_id' => $child_id, 
            'child_name' => $child_info['nick'],
            'child_img' => $child_info['head_img_url'],
            'gender' => $child_info['gender'],
            'grade' => $grade_array[$child_info['grade']],
            'stars' => $child_info['stars'],
            'zuoye_stars' => $child_info['zuoye_stars'],
            'zuoye_stars_a' => $child_info['zuoye_stars_a'],
            'zuoye_stars_b' => $child_info['zuoye_stars_b'],
            'zuoye_stars_c' => $child_info['zuoye_stars_c'],
            'habit_count' => $habit_count,
        );
        $this->smarty->assign("child_info", $info);



        //获取用户习惯
        $task_list_data = $this->get_today_habit($child_id);
        // print_r($task_list_data);exit;

        $this->smarty->assign("time_line", $task_list_data['time_line']);
        $this->smarty->assign("task_list", $task_list_data['list']);
        
		$this->smarty->display("h5/habit_home.html");
	}


	function detail($habit_id){
        $this->load->model("Habits");
        $habit_info = get_field_by_where("task_types","id, name, image, remark","id='{$habit_id}'");
        if(!$habit_info){
            show_error("404 data not defined");
            exit;
        }
        $this->smarty->assign("habit_info", $habit_info);
		$this->smarty->display("h5/habit_detail.html");
	}


	private function get_today_habit($child_id){
       
        $this->load->model("Tasks");
       
        $pid = !empty($data['pid']) ? $data['pid'] : 0;
        $week = date('N', time());
        $task_list = $this->Tasks->get_task_list(0,0,array("pid" => $pid, "child_id"=>$child_id, "find_ini_set" => $week));
        $task_list_data = array('count' => 0, 'list' => array());

        if(!empty($task_list['count'])){
            $task_list_data['count'] = $task_list['count'];
            foreach ($task_list['list'] as $key => $value) {
                $statistics_info = array("total_stars" => 0, "continuity" => 0);
                $statistics = get_field_by_where("user_children_task_type_statistics", "*", "task_id='{$value['id']}'");
                if($statistics){
                    $statistics_info['continuity'] = $statistics['continuity'];
                }

                $last_continuity = get_field_by_where("user_children_max_continuity_record", "end_time", "task_id='{$value['id']}' AND child_id = '{$child_id}' ORDER BY id DESC");
                $today_time = strtotime(date("Y-m-d", time()));
                $yesterday_time = $today_time - 3600 * 24;
                if($last_continuity){
                    if($last_continuity['end_time'] >= $yesterday_time && $last_continuity['end_time'] < $today_time){
                        $statistics_info['continuity'] = $statistics_info['continuity'];
                    }elseif($last_continuity['end_time'] < $today_time){
                        $statistics_info['continuity'] = 0;
                    }
                }
                $value['continuity'] = $statistics_info['continuity'];
                if(!empty($value['task_time'])){
                    $task_list_data['time_line'][] = $value;
                }else{
                    $task_list_data['list'][] = $value;
                }
            }
        }
        return $task_list_data;
    }

    //获取习惯的推荐阅读
    function get_recommend_article(){
        $data = file_get_contents("php://input");
        $data = json_decode($data, true);
        $this->load->model("Habits");
        $recommend_list = $this->Habits->get_recommend_list($data['task_type_id']);
        $data_obj = array(
                'count' => count($recommend_list),
                'list' => $recommend_list
             );
        echo json_encode(array("code" => 1, "data" => $data_obj));
    }

    function get_user_dynamic(){
        $data = file_get_contents("php://input");
        $data = json_decode($data, true);
        $this->load->model("Habits");
        $task_info = $this->Habits->get_habit_info($data['habit_id']);
        if(!$task_info){
            echo json_encode(array("code" => 0));
            exit;
        }
        $conditions['type'] = 1;
        $conditions['category_id'] = $task_info['task_type_id'];
        $conditions['child_id'] = $task_info['child_id'];
        $this->load->model("User_child_model");
        $limit = ($data['page'] - 1) * $this->offset;
        $user_dynamic = $this->User_child_model->get_child_dynamics($limit, $this->offset, $conditions);
        $user_dynamic['total_page'] = ceil($user_dynamic['count'] / $this->offset);
        foreach ($user_dynamic['list'] as $key => $value) {
            $text = "坚持 #{$value['name']}# " . "第{$value['day']}天<br/>";
            $user_dynamic['list'][$key]['text'] = $text . $user_dynamic['list'][$key]['text'];
        }
        echo json_encode(array("code" => 1, "data" => $user_dynamic));
    }

    function get_habit_dynamic(){
        $data = file_get_contents("php://input");
        $data = json_decode($data, true);
        $this->load->model("Habits");
        $conditions['type'] = 1;
        $conditions['category_id'] = $data['task_type_id'];
        $this->load->model("User_child_model");
        $limit = ($data['page'] - 1) * $this->offset;
        $user_dynamic = $this->User_child_model->get_child_dynamics($limit, $this->offset, $conditions);
        $user_dynamic['total_page'] = ceil($user_dynamic['count'] / $this->offset);
        foreach ($user_dynamic['list'] as $key => $value) {
            $text = "坚持 #{$value['name']}# " . "第{$value['day']}天<br/>";
            $user_dynamic['list'][$key]['text'] = $text . $user_dynamic['list'][$key]['text'];
            if(empty($value['head_img_url'])){
                if($value['gender'] == 1){
                    $user_dynamic['list'][$key]['head_img_url'] = get_qiniu_url('FtE8Gn7PqWv0W7aVTuab8RcuGxlm');
                }else{
                    $user_dynamic['list'][$key]['head_img_url'] = get_qiniu_url('Fsof6z54HyIM401i0amzqo3VzrF3');
                }
            }
        }
        echo json_encode(array("code" => 1, "data" => $user_dynamic));
    }
}
